package com.hskn.hss.module.tlparkexp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlparkexp.entity.TlParkExp;
import com.hskn.hss.module.tlparkexp.mapper.TlParkExpMapper;
import com.hskn.hss.module.tlparkexp.service.ITlParkExpService;
import com.hskn.hss.module.tlparktreeexp.entity.TlParkTreeExp;
import com.hskn.hss.module.tlparktreeexp.mapper.TlParkTreeExpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-13
 */
@Service
public class TlParkExpServiceImpl extends ServiceImpl<TlParkExpMapper, TlParkExp> implements ITlParkExpService {
    @Autowired
    TlParkExpMapper tlParkExpMapper;
    @Autowired
    TlParkTreeExpMapper tlParkTreeExpMapper;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}) //添加事务
    public void insert(TlParkExp tlParkExp) throws HssException {
        tlParkExpMapper.insert(tlParkExp);
        Map map = new HashMap();
        map.put("maintbid", tlParkExp.getId());

        tlParkTreeExpMapper.deleteByMap(map);
        List<TlParkTreeExp> treeList = tlParkExp.getDetails();
        if (treeList!=null&&!treeList.isEmpty()) {
            for (TlParkTreeExp treeExp : treeList) {
                treeExp.setMaintbid(tlParkExp.getId());
                tlParkTreeExpMapper.insert(treeExp);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}) //添加事务
    public void deletebymaintbid(String maintbid) throws HssException {
        QueryWrapper<TlParkExp> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlParkExp::getMaintbid, maintbid);
        TlParkExp tlParkExp = tlParkExpMapper.selectOne(queryWrapper);
        Map submap = new HashMap();
        submap.put("maintbid", tlParkExp.getId());
        tlParkTreeExpMapper.deleteByMap(submap);
        Map map = new HashMap();
        map.put("maintbid", maintbid);
        tlParkExpMapper.deleteByMap(map);


    }

    @Override
    public List<TlParkExp> getDetail(String maintbid) throws HssException {
        Map map = new HashMap();
        map.put("maintbid", maintbid);
        List<TlParkExp> resultList = tlParkExpMapper.selectByMap(map);
        for (TlParkExp tlParkExp : resultList) {
            Map map2 = new HashMap();
            map2.put("maintbid", tlParkExp.getId());
            tlParkExp.setDetails(tlParkTreeExpMapper.selectByMap(map2));
        }
        return resultList;
    }
}
